package com.changgou.goods.controller;

import com.changgou.goods.pojo.Album;
import com.changgou.goods.service.AlbumService;
import com.github.pagehelper.PageInfo;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Album控制器
 *
 * @author mario
 * @version 1.0
 * @date 2020/11/13 15:39
 */
@RestController
@RequestMapping("/album")
@CrossOrigin
public class AlbumController {

  @Autowired private AlbumService albumService;

  /***
   * Album分页条件搜索实现
   * @param album
   * @param page
   * @param size
   * @return
   */
  @PostMapping(value = "/search/{page}/{size}")
  public Result<PageInfo> findPage(
      @RequestBody(required = false) Album album, @PathVariable int page, @PathVariable int size) {
    // 调用AlbumService实现分页条件查询Album
    PageInfo<Album> pageInfo = albumService.findPage(album, page, size);
    return new Result(true, StatusCode.OK, "查询成功", pageInfo);
  }

  /***
   * Album分页搜索实现
   * @param page:当前页
   * @param size:每页显示多少条
   * @return
   */
  @GetMapping(value = "/search/{page}/{size}")
  public Result<PageInfo> findPage(@PathVariable int page, @PathVariable int size) {
    // 调用AlbumService实现分页查询Album
    PageInfo<Album> pageInfo = albumService.findPage(page, size);
    return new Result<PageInfo>(true, StatusCode.OK, "查询成功", pageInfo);
  }

  /***
   * 多条件搜索数据
   * @param album
   * @return
   */
  @PostMapping(value = "/search")
  public Result<List<Album>> findList(@RequestBody(required = false) Album album) {
    // 调用AlbumService实现条件查询Album
    List<Album> list = albumService.findList(album);
    return new Result<List<Album>>(true, StatusCode.OK, "查询成功", list);
  }

  /***
   * 根据ID删除数据
   * @param id
   * @return
   */
  @DeleteMapping(value = "/{id}")
  public Result delete(@PathVariable Long id) {
    // 调用AlbumService实现根据主键删除
    albumService.delete(id);
    return new Result(true, StatusCode.OK, "删除成功");
  }

  /***
   * 修改Album数据
   * @param album
   * @param id
   * @return
   */
  @PutMapping(value = "/{id}")
  public Result update(@RequestBody Album album, @PathVariable Long id) {
    // 设置主键值
    album.setId(id);
    // 调用AlbumService实现修改Album
    albumService.update(album);
    return new Result(true, StatusCode.OK, "修改成功");
  }

  /***
   * 新增Album数据
   * @param album
   * @return
   */
  @PostMapping
  public Result add(@RequestBody Album album) {
    // 调用AlbumService实现添加Album
    albumService.add(album);
    return new Result(true, StatusCode.OK, "添加成功");
  }

  /***
   * 根据ID查询Album数据
   * @param id
   * @return
   */
  @GetMapping("/{id}")
  public Result<Album> findById(@PathVariable Long id) {
    // 调用AlbumService实现根据主键查询Album
    Album album = albumService.findById(id);
    return new Result<Album>(true, StatusCode.OK, "查询成功", album);
  }

  /***
   * 查询Album全部数据
   * @return
   */
  @GetMapping
  public Result<List<Album>> findAll() {
    // 调用AlbumService实现查询所有Album
    List<Album> list = albumService.findAll();
    return new Result<List<Album>>(true, StatusCode.OK, "查询成功", list);
  }
}
